/**
 * Represents a JSON value, which can be a primitive value, object, or array
 */
export type JSONValue = JSONValue.Primitive | JSONValue.Object | JSONValue.Array;

export namespace JSONValue {
  /**
   * Represents primitive value types in JSON, including string, number, boolean, and null
   */
  export type Primitive = string | number | boolean | null;

  /**
   * Represents a JSON object with string keys and arbitrary JSON values
   */
  export type Object = {
    [key: string]: JSONValue;
  };

  /**
   * Represents a JSON array with elements of arbitrary JSON values
   */
  export type Array = JSONValue[];
}

/**
 * Represents a standardized content segment transmitted between client, MCP, and model,
 * with different types of segments having specific semantics
 */
export type Part =
  | Part.Text
  | Part.File
  | Part.Reference
  | Part.Error
  | Part.Resource
  | Part.ToolCall
  | Part.ToolResult
  | Part.Reasoning
  | Part.Source;

export namespace Part {
  /**
   * Represents plain text content, the most basic content type
   */
  export type Text = {
    /**
     * Type; fixed as "text"; identifies this as a text content segment
     */
    type: "text";
    /**
     * Actual text content string
     */
    text: string;
  };

  /**
   * Represents file content, typically used for files selected by the user, generated by the model,
   * or resources without URI returned by MCP such as Image or Audio type resources.
   */
  export type File = {
    /**
     * Type; fixed as "file"; identifies this as a file content segment
     */
    type: "file";
    /**
     * Name of the file
     */
    name?: string;
    /**
     * Media type of the file, used to identify the file format
     */
    mimetype: string;
    /**
     * File content as binary data
     */
    content: Uint8Array;
  };

  /**
   * Represents a resource that has been loaded into memory with a resource identifier (URI).
   * Use this when the resource can be referenced via a Reference; otherwise, use File.
   */
  export type Resource = {
    /**
     * Type; fixed as "resource"; identifies this as a resource content segment
     */
    type: "resource";
    /**
     * URL of the resource; points to its source location
     */
    url: string;
    /**
     * Media type of the resource
     */
    mimetype: string;
    /**
     * Resource content; when type is string, it represents textual content,
     * otherwise it represents binary content
     */
    content: string | Uint8Array;
  };

  /**
   * Represents the model's internal reasoning process
   */
  export type Reasoning = {
    /**
     * Type; fixed as "reasoning"; identifies this as a reasoning content segment
     */
    type: "reasoning";
    /**
     * Text description of the reasoning process
     */
    text: string;
  };

  /**
   * Represents a tool call request containing the required parameter information for invocation
   */
  export type ToolCall = {
    /**
     * Type; fixed as "tool-call"; identifies this as a tool call content segment
     */
    type: "tool-call";
    /**
     * Unique identifier for the tool call; used to match tool calls with tool results
     */
    id: string;
    /**
     * Tool name; the name of the tool to be invoked
     */
    tool: string;
    /**
     * Tool input arguments
     */
    arguments: JSONValue;
  };

  /**
   * Represents the return result after tool execution, which may include success or failure status
   */
  export type ToolResult = {
    /**
     * Type; fixed as "tool-result"; identifies this as a tool result content segment
     */
    type: "tool-result";
    /**
     * Unique identifier for the tool call; matches the corresponding `ToolCall.id`
     */
    id: string;
    /**
     * Tool name; the name of the tool that produced this result
     */
    tool: string;
    /**
     * Tool input arguments
     */
    arguments: JSONValue;
    /**
     * Array of result content segments returned after tool execution, which can contain multiple types of content
     */
    result: Array<Text | File | Reference | Error | Resource>;
    /**
     * Structured output data
     */
    structuredResult?: JSONValue;
    /**
     * Execution status; the tool's output result should also correspond to the content
     */
    status: "success" | "failure";
  };

  /**
   * Represents a reference to an external resource, such as a web link, knowledge base document, or MCP resource
   */
  export type Reference = {
    /**
     * Type; fixed as "reference"; identifies this as a reference content segment
     */
    type: "reference";
    /**
     * URL of the referenced resource; points to the location of the referenced resource
     */
    url: string;
    /**
     * Optional title of the referenced resource
     */
    title?: string;
    /**
     * Description of the resource
     */
    description?: string;
    /**
     * Optional media type of the resource
     */
    mimetype?: string;
  };

  /**
   * Represents an error message containing error information
   */
  export type Error = {
    /**
     * Type; fixed as "error"; identifies this as an error content segment
     */
    type: "error";
    /**
     * Error message
     */
    message: string;
  };

  /**
   * Represents a source content used to identify the information source;
   * models may return this type of content segment when capabilities like WebSearch are enabled
   */
  export type Source = {
    /**
     * Type; fixed as "source"; identifies this as a source content segment
     */
    type: "source";
    /**
     * URL of the source resource; points to the location of the referenced resource
     */
    url: string;
    /**
     * Optional title of the source resource
     */
    title?: string;
  };
}

/**
 * Represents a message containing role and content
 */
export type Message =
  | {
      /**
       * Role; fixed value "user"; represents a user message
       */
      role: "user";
      /**
       * Message content
       */
      content: Array<Part.Text | Part.File | Part.Reference | Part.Resource>;
    }
  | {
      /**
       * Role; fixed value "assistant"; represents a model response message
       */
      role: "assistant";
      /**
       * Message content
       */
      content: Array<
        | Part.Text
        | Part.File
        | Part.Reference
        | Part.Reasoning
        | Part.ToolCall
        | Part.Error
        | Part.Source
        | Part.Resource
      >;
    }
  | {
      /**
       * Role; fixed value "system"; represents a system message
       */
      role: "system";
      /**
       * System message content
       */
      content: Array<Part.Text | Part.Reference>;
    }
  | {
      /**
       * Role; fixed value "tool"; represents a tool message
       */
      role: "tool";
      /**
       * Tool message content
       */
      content: Array<Part.ToolResult>;
    };
